package com.wuchu.shiro.controller;

import com.wuchu.shiro.pojo.User;
import com.wuchu.shiro.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * 类描述：
 * Author: WuChu
 * Date: 2022/3/3
 * Version: 1.0
 */


/*
 * @RestController跟@Controller的区别：
 * 前者返回字符串，后者返回界面
 */
@Slf4j
@Controller
public class MyController {

    @Autowired
    UserService userService;

    @RequestMapping({"/","/index"})
    public  String index(Model model){
        model.addAttribute("msg","Hello Shiro");
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(Model model){
        model.addAttribute("msg","Hello Shiro");
        return "login";
    }

    @RequiresPermissions("user:add")
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "user/add";
    }

    @RequiresPermissions("user:update")
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,int id){
       User user = userService.queryById(id);
       model.addAttribute("user",user);
        return "user/update";
    }


    @RequiresPermissions("user:add")
    @RequestMapping("/user/add")
    public String  add(Model model,String username, String password,String perms){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPerms(perms);
        userService.add(user);
        System.out.println("执行了add");
        model.addAttribute("info","添加用户"+ user.getUsername() +"成功！！");
        List<User> userList = userService.queryAll();
        model.addAttribute("userList",userList);
        return "index";
    }

    @RequiresPermissions("user:delete")
    @RequestMapping("/user/delete")
    public String delete(Model model, int id){
        User user = userService.queryById(id);
        model.addAttribute("info","删除用户"+user.getUsername()+"成功！！");
        userService.delete(id);
        List<User> userList = userService.queryAll();
        model.addAttribute("userList",userList);
        return "index";
    }


    @RequiresPermissions("user:update")
    @RequestMapping("/user/update")
    public String update(Model model,User user){
       userService.updateUser(user);
        List<User> userList = userService.queryAll();
        model.addAttribute("userList",userList);
        model.addAttribute("info","更新"+user.getUsername()+"信息成功！！");
        return "index";
    }

    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        //用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
        } catch (UnknownAccountException e) {
            model.addAttribute("msg","用户不存在！");
            return "login";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg","密码错误！");
            return "login";
        }
        User user = userService.queryByName(username);
        model.addAttribute("info","欢迎"+user.getUsername()+"进入系统,你的权限是"+user.getPerms());

        List<User> userList = userService.queryAll();
        model.addAttribute("userList",userList);
        return "index";
    }





}
